{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import lasio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "las = lasio.read(os.path.join('..', 'tests', 'examples', 'sample.las'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "we'll use this LAS file as an example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "~VERSION INFORMATION\n",
      " VERS.                  1.2:   CWLS LOG ASCII STANDARD -VERSION 1.2\n",
      " WRAP.                  NO:   ONE LINE PER DEPTH STEP\n",
      "~WELL INFORMATION BLOCK\n",
      "#MNEM.UNIT       DATA TYPE    INFORMATION\n",
      "#---------    -------------   ------------------------------\n",
      " STRT.M        1670.000000:\n",
      " STOP.M        1660.000000:\n",
      " STEP.M            -0.1250:\n",
      " NULL.           -999.2500:\n",
      " COMP.             COMPANY:   # ANY OIL COMPANY LTD.\n",
      " WELL.                WELL:   ANY ET AL OIL WELL #12\n",
      " FLD .               FIELD:   EDAM\n",
      " LOC .            LOCATION:   A9-16-49-20W3M\n",
      " PROV.            PROVINCE:   SASKATCHEWAN\n",
      " SRVC.     SERVICE COMPANY:   ANY LOGGING COMPANY LTD.\n",
      " DATE.            LOG DATE:   25-DEC-1988\n",
      " UWI .      UNIQUE WELL ID:   100091604920W300\n",
      "~CURVE INFORMATION\n",
      "#MNEM.UNIT      API CODE      CURVE DESCRIPTION\n",
      "#---------    -------------   ------------------------------\n",
      " DEPT.M                      :  1  DEPTH\n",
      " DT  .US/M     \t\t     :  2  SONIC TRANSIT TIME\n",
      " RHOB.K/M3                   :  3  BULK DENSITY\n",
      " NPHI.V/V                    :  4   NEUTRON POROSITY\n",
      " SFLU.OHMM                   :  5  RXO RESISTIVITY\n",
      " SFLA.OHMM                   :  6  SHALLOW RESISTIVITY\n",
      " ILM .OHMM                   :  7  MEDIUM RESISTIVITY\n",
      " ILD .OHMM                   :  8  DEEP RESISTIVITY\n",
      "~PARAMETER INFORMATION\n",
      "#MNEM.UNIT        VALUE       DESCRIPTION\n",
      "#---------    -------------   ------------------------------\n",
      " BHT .DEGC         35.5000:   BOTTOM HOLE TEMPERATURE\n",
      " BS  .MM          200.0000:   BIT SIZE\n",
      " FD  .K/M3       1000.0000:   FLUID DENSITY\n",
      " MATR.              0.0000:   NEUTRON MATRIX(0=LIME,1=SAND,2=DOLO)\n",
      " MDEN.           2710.0000:   LOGGING MATRIX DENSITY\n",
      " RMF .OHMM          0.2160:   MUD FILTRATE RESISTIVITY\n",
      " DFD .K/M3       1525.0000:   DRILL FLUID DENSITY\n",
      "~Other\n",
      "     Note: The logging tools became stuck at 625 meters causing the data\n",
      "\t   between 625 meters and 615 meters to be invalid.\n",
      "~A  DEPTH     DT       RHOB     NPHI     SFLU     SFLA      ILM      ILD\n",
      "1670.000   123.450 2550.000    0.450  123.450  123.450  110.200  105.600\n",
      "1669.875   123.450 2550.000    0.450  123.450  123.450  110.200  105.600\n",
      "1669.750   123.450 2550.000    0.450  123.450  123.450  110.200  105.600\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print las._text"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now use the ``ExcelConverter`` object to produce an Excel spreadsheet:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "lasio.ExcelConverter(las).write('example.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "we can import this spreadsheet back into Python directly using ``pandas``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The spreadsheet has two sheets, one called \"Header\" with the header information from each section:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "xls_header_sheet = pandas.read_excel('example.xlsx', sheetname='Header')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Section</th>\n",
       "      <th>Mnemonic</th>\n",
       "      <th>Unit</th>\n",
       "      <th>Value</th>\n",
       "      <th>Description</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>~Version</td>\n",
       "      <td>VERS</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.2</td>\n",
       "      <td>CWLS LOG ASCII STANDARD -VERSION 1.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>~Version</td>\n",
       "      <td>WRAP</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NO</td>\n",
       "      <td>ONE LINE PER DEPTH STEP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>~Well</td>\n",
       "      <td>STRT</td>\n",
       "      <td>M</td>\n",
       "      <td>1670</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>~Well</td>\n",
       "      <td>STOP</td>\n",
       "      <td>M</td>\n",
       "      <td>1660</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>~Well</td>\n",
       "      <td>STEP</td>\n",
       "      <td>M</td>\n",
       "      <td>-0.125</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>~Well</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-999.25</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>~Well</td>\n",
       "      <td>COMP</td>\n",
       "      <td>NaN</td>\n",
       "      <td># ANY OIL COMPANY LTD.</td>\n",
       "      <td>COMPANY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>~Well</td>\n",
       "      <td>WELL</td>\n",
       "      <td>NaN</td>\n",
       "      <td>ANY ET AL OIL WELL #12</td>\n",
       "      <td>WELL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>~Well</td>\n",
       "      <td>FLD</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EDAM</td>\n",
       "      <td>FIELD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>~Well</td>\n",
       "      <td>LOC</td>\n",
       "      <td>NaN</td>\n",
       "      <td>A9-16-49-20W3M</td>\n",
       "      <td>LOCATION</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>~Well</td>\n",
       "      <td>PROV</td>\n",
       "      <td>NaN</td>\n",
       "      <td>SASKATCHEWAN</td>\n",
       "      <td>PROVINCE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>~Well</td>\n",
       "      <td>SRVC</td>\n",
       "      <td>NaN</td>\n",
       "      <td>ANY LOGGING COMPANY LTD.</td>\n",
       "      <td>SERVICE COMPANY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>~Well</td>\n",
       "      <td>DATE</td>\n",
       "      <td>NaN</td>\n",
       "      <td>25-DEC-1988</td>\n",
       "      <td>LOG DATE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>~Well</td>\n",
       "      <td>UWI</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100091604920W300</td>\n",
       "      <td>UNIQUE WELL ID</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>~Parameter</td>\n",
       "      <td>BHT</td>\n",
       "      <td>DEGC</td>\n",
       "      <td>35.5</td>\n",
       "      <td>BOTTOM HOLE TEMPERATURE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>~Parameter</td>\n",
       "      <td>BS</td>\n",
       "      <td>MM</td>\n",
       "      <td>200</td>\n",
       "      <td>BIT SIZE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>~Parameter</td>\n",
       "      <td>FD</td>\n",
       "      <td>K/M3</td>\n",
       "      <td>1000</td>\n",
       "      <td>FLUID DENSITY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>~Parameter</td>\n",
       "      <td>MATR</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NEUTRON MATRIX(0=LIME,1=SAND,2=DOLO)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>~Parameter</td>\n",
       "      <td>MDEN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2710</td>\n",
       "      <td>LOGGING MATRIX DENSITY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>~Parameter</td>\n",
       "      <td>RMF</td>\n",
       "      <td>OHMM</td>\n",
       "      <td>0.216</td>\n",
       "      <td>MUD FILTRATE RESISTIVITY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>~Parameter</td>\n",
       "      <td>DFD</td>\n",
       "      <td>K/M3</td>\n",
       "      <td>1525</td>\n",
       "      <td>DRILL FLUID DENSITY</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Section Mnemonic  Unit                     Value  \\\n",
       "0     ~Version     VERS   NaN                       1.2   \n",
       "1     ~Version     WRAP   NaN                        NO   \n",
       "2        ~Well     STRT     M                      1670   \n",
       "3        ~Well     STOP     M                      1660   \n",
       "4        ~Well     STEP     M                    -0.125   \n",
       "5        ~Well      NaN   NaN                   -999.25   \n",
       "6        ~Well     COMP   NaN    # ANY OIL COMPANY LTD.   \n",
       "7        ~Well     WELL   NaN    ANY ET AL OIL WELL #12   \n",
       "8        ~Well      FLD   NaN                      EDAM   \n",
       "9        ~Well      LOC   NaN            A9-16-49-20W3M   \n",
       "10       ~Well     PROV   NaN              SASKATCHEWAN   \n",
       "11       ~Well     SRVC   NaN  ANY LOGGING COMPANY LTD.   \n",
       "12       ~Well     DATE   NaN               25-DEC-1988   \n",
       "13       ~Well      UWI   NaN          100091604920W300   \n",
       "14  ~Parameter      BHT  DEGC                      35.5   \n",
       "15  ~Parameter       BS    MM                       200   \n",
       "16  ~Parameter       FD  K/M3                      1000   \n",
       "17  ~Parameter     MATR   NaN                         0   \n",
       "18  ~Parameter     MDEN   NaN                      2710   \n",
       "19  ~Parameter      RMF  OHMM                     0.216   \n",
       "20  ~Parameter      DFD  K/M3                      1525   \n",
       "\n",
       "                             Description  \n",
       "0   CWLS LOG ASCII STANDARD -VERSION 1.2  \n",
       "1                ONE LINE PER DEPTH STEP  \n",
       "2                                    NaN  \n",
       "3                                    NaN  \n",
       "4                                    NaN  \n",
       "5                                    NaN  \n",
       "6                                COMPANY  \n",
       "7                                   WELL  \n",
       "8                                  FIELD  \n",
       "9                               LOCATION  \n",
       "10                              PROVINCE  \n",
       "11                       SERVICE COMPANY  \n",
       "12                              LOG DATE  \n",
       "13                        UNIQUE WELL ID  \n",
       "14               BOTTOM HOLE TEMPERATURE  \n",
       "15                              BIT SIZE  \n",
       "16                         FLUID DENSITY  \n",
       "17  NEUTRON MATRIX(0=LIME,1=SAND,2=DOLO)  \n",
       "18                LOGGING MATRIX DENSITY  \n",
       "19              MUD FILTRATE RESISTIVITY  \n",
       "20                   DRILL FLUID DENSITY  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xls_header_sheet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and another called \"Curves\" containing the data itself:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "xls_data_sheet = pandas.read_excel('example.xlsx', sheetname='Curves')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>DEPT</th>\n",
       "      <th>DT</th>\n",
       "      <th>RHOB</th>\n",
       "      <th>NPHI</th>\n",
       "      <th>SFLU</th>\n",
       "      <th>SFLA</th>\n",
       "      <th>ILM</th>\n",
       "      <th>ILD</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1670.000</td>\n",
       "      <td>123.45</td>\n",
       "      <td>2550</td>\n",
       "      <td>0.45</td>\n",
       "      <td>123.45</td>\n",
       "      <td>123.45</td>\n",
       "      <td>110.2</td>\n",
       "      <td>105.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1669.875</td>\n",
       "      <td>123.45</td>\n",
       "      <td>2550</td>\n",
       "      <td>0.45</td>\n",
       "      <td>123.45</td>\n",
       "      <td>123.45</td>\n",
       "      <td>110.2</td>\n",
       "      <td>105.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1669.750</td>\n",
       "      <td>123.45</td>\n",
       "      <td>2550</td>\n",
       "      <td>0.45</td>\n",
       "      <td>123.45</td>\n",
       "      <td>123.45</td>\n",
       "      <td>110.2</td>\n",
       "      <td>105.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       DEPT      DT  RHOB  NPHI    SFLU    SFLA    ILM    ILD\n",
       "0  1670.000  123.45  2550  0.45  123.45  123.45  110.2  105.6\n",
       "1  1669.875  123.45  2550  0.45  123.45  123.45  110.2  105.6\n",
       "2  1669.750  123.45  2550  0.45  123.45  123.45  110.2  105.6"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xls_data_sheet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to further modify the ``openpyxl.Workbook`` object, you can access it like so:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "converter = lasio.ExcelConverter(las)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "openpyxl.workbook.workbook.Workbook"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(converter.workbook)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "finally take a look at some screenshots of the Excel file itself:\n",
    "\n",
    "![](example.xlsx-Header.png)\n",
    "\n",
    "![](example.xlsx-Curves.png)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
